﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;

namespace LicPdaService
{
    public class Putaway
    {
        /// <summary>
        /// 2.1原料上架打印
        /// DataSource:Orders
        /// </summary>
        public static string GetPutaway(string xml)
        {
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            App.Connection();
            App.DBconSet();

            //1接收上架XML
            DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
            if (ldt.Rows.Count == 0)
            {
                return "";
            }

            //2拆分组合新的ASN上架表--获取库位
            DataTable ldt_sj = LicPdaService.App.GetNewDataTabel(ldt);
            //3获取库位
            //3.1补充信息customer_code,test
            strSql = "select customer_code from Receipt where receiptkey='" + ldt_sj.Rows[0]["asn"].ToString() + "' AND whseid='"+ ldt_sj.Rows[0]["whseid"].ToString() + "'";
            DataTable ldt_customer_code = App.GetDataTable(App.DBconWMS, strSql, "Sku");
            for (int i = 0; i < ldt_sj.Rows.Count; i++)
            {
                if (ldt_customer_code.Rows.Count > 0)
                {
                    ldt_sj.Rows[i]["customer_code"] = ldt_customer_code.Rows[0]["customer_code"].ToString();
                }
                strSql = "select test from sku where sku='" + ldt_sj.Rows[i]["sku"].ToString() + "'";
                DataTable ldt_test = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                if (ldt_test.Rows.Count > 0)
                {
                    ldt_sj.Rows[i]["test"] = ldt_test.Rows[0]["test"].ToString();
                }
            }
            string lskw = App.GetPutAway(ldt_sj);
            if (lskw != "")
            {
                for (int i = 0; i < ldt_sj.Rows.Count; i++)
                {
                    ldt_sj.Rows[i]["storage"] = lskw;
                }

            }
            return GetPutaway_Return(ldt_sj);
        }

        //上架--分配库位返回值
        private static string GetPutaway_Return(DataTable ldt)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement rootRet = doc.CreateElement("NewDataSet");

            for (int i = 0; i < ldt.Rows.Count; i++)
            {
                XmlNode putawayNodes = doc.CreateElement("putaway_return");//返回用户节点
                XmlElement lxeuserid = doc.CreateElement("userid");
                XmlElement lxeasn = doc.CreateElement("asn");
                XmlElement lxewhseid = doc.CreateElement("whseid");
                XmlElement lxemaster_label = doc.CreateElement("master_label");
                XmlElement lxesku = doc.CreateElement("sku");
                XmlElement lxeqty = doc.CreateElement("qty");
                XmlElement lxestorage = doc.CreateElement("storage");
                XmlElement lxereturn_status = doc.CreateElement("return_status");

                lxeuserid.InnerText = ldt.Rows[i]["userid"].ToString();
                lxeasn.InnerText = ldt.Rows[i]["asn"].ToString();
                lxewhseid.InnerText = ldt.Rows[i]["whseid"].ToString();
                lxemaster_label.InnerText = ldt.Rows[i]["master_label"].ToString();
                lxesku.InnerText = ldt.Rows[i]["sku"].ToString();
                lxeqty.InnerText = ldt.Rows[i]["qty"].ToString();
                lxestorage.InnerText = ldt.Rows[i]["storage"].ToString();
                lxereturn_status.InnerText = ldt.Rows[i]["msg"].ToString();
                putawayNodes.AppendChild(lxeuserid);
                putawayNodes.AppendChild(lxeasn);
                putawayNodes.AppendChild(lxewhseid);
                putawayNodes.AppendChild(lxemaster_label);
                putawayNodes.AppendChild(lxesku);
                putawayNodes.AppendChild(lxestorage);
                putawayNodes.AppendChild(lxereturn_status);
                rootRet.AppendChild(putawayNodes);
            }


            doc.AppendChild(rootRet);
            return doc.InnerXml;
        }


        public static string Print(string xml)
        {
            List<string> sqllists = new List<string>();
            Check.status_check = "true";
            string strSql = string.Empty;
            string lspn = string.Empty;
            string sku = string.Empty;
            string asn = string.Empty;
            int qty = 0;
            int qty_yb = 0;
            int qty_sm = 0;
            int qty_ybsm = 0;
            App.Connection();
            DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
            if (ldt.Rows.Count == 0)
            {
                return "";
            }
            App.swhseid = ldt.Rows[0]["whseid"].ToString();
            App.suserid = ldt.Rows[0]["userid"].ToString();
            App.sasn = ldt.Rows[0]["asn"].ToString();

            strSql = "select recstatus.status,receipt.shipkey,data_from from receipt left join recstatus on receipt.status=recstatus.status where recstatus.status in('01','02','03','04','05','06')  and receipt.whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "' ";
            DataTable ldt_asn = App.GetDataTable(App.DBconWMS, strSql, "Sku");

            strSql = "select * from  ReceiptDetail where whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "'";
            DataTable ldt_asn_detail_ys = App.GetDataTable(App.DBconWMS, strSql, "Sku");

            if (ldt.Rows[0]["check"].ToString().ToUpper() == "TRUE")
            {
                try
                {
                    //验收
                    DataTable ldt_ys = Check.GetCheck_Logic(ldt, ldt_asn, ldt_asn_detail_ys, "");
                    for (int i = 0; i < ldt_ys.Rows.Count; i++)
                    {
                        if (ldt_ys.Rows[i]["msg"].ToString().Contains("送货数量") == false)
                        {
                            return GetPrint_Return(ldt_ys);
                        }
                    }

                }
                catch (Exception ex)
                {
                    App.Log("Print", ex.Message.ToString());
                    throw;
                }
            }


            //2017-04-06增加，先验收完成后，再判断上架信息
            strSql = "select * from  ReceiptDetail where (status in ('01','02','03','04','05') OR status IS NULL) and  whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "'";
            DataTable ldt_asn_detail = App.GetDataTable(App.DBconWMS, strSql, "Sku");

            DataTable ldt_dy = App.GetNewDataTabel(ldt);
            if (ldt_asn_detail.Rows.Count == 0)
            {
                for (int j = 0; j < ldt_dy.Rows.Count; j++)
                {
                    ldt_dy.Rows[j]["msg"] = "ASN中未发现要上架的信息！ 可能原因：1、登陆账号的厂别" + App.swhseid + "错误  2、无ASN预报";
                }
                return GetPrint_Return(ldt_dy);
            }

            #region 4.1 新增预报信息量与上架实物扫描数量匹配逻辑 quinn
            DataTable ldt_sj = LicPdaService.App.GetCreateDataTable();
            if (ldt.Rows[0]["check"].ToString().ToUpper() != "TRUE")
            {
                try
                {
                    #region 按料号+订单号+批次 汇总
                    for (int i = 0; i < ldt.Rows.Count; i++)
                    {

                        string[] ls;
                        ls = ldt.Rows[i]["master_label"].ToString().Split('|');
                        if (ls.Length == 5 || ls.Length == 4)
                        {
                            DataRow[] ldrs = null;
                            if (ls.Length == 5)
                            {
                                string lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "' and lottable02='" + ls[1].ToString() + "' and lottable01='" + ls[2].ToString() + "' and shipkey='" + ls[4].ToString() + "'";
                                if (ldt_asn.Rows[0]["data_from"].ToString() == "CMS")
                                {
                                    lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "' and lottable02='" + ls[1].ToString() + "'  and shipkey='" + ls[4].ToString() + "'";
                                }
                                ldrs = ldt_sj.Select(lswhere);
                            }
                            else if (ls.Length == 4)
                            {
                                string lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "' and lottable02='" + ls[1].ToString() + "' and lottable01='" + ls[2].ToString() + "' ";
                                if (ldt_asn.Rows[0]["data_from"].ToString() == "CMS")
                                {
                                    lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "' and lottable02='" + ls[1].ToString() + "'  ";
                                }
                                ldrs = ldt_sj.Select(lswhere);
                            }
                            if (ldrs.Length == 0)
                            {
                                DataRow ldr = ldt_sj.NewRow();
                                ldr["asn"] = App.sasn;
                                ldr["sku"] = ls[0].ToString();
                                ldr["lottable02"] = ls[1].ToString();
                                ldr["lottable01"] = ls[2].ToString();
                                if (ls.Length == 5)
                                {
                                    ldr["shipkey"] = ls[4].ToString();
                                }
                                else
                                {
                                    ldr["shipkey"] = ldt_asn.Rows[0]["shipkey"].ToString();
                                }
                                ldr["qty"] = Convert.ToSingle(ls[3].ToString());
                                ldr["master_label"] = ldt.Rows[i]["master_label"].ToString();
                                ldr["userid"] = App.suserid;
                                ldr["whseid"] = App.swhseid;
                                ldt_sj.Rows.Add(ldr);
                            }
                            else
                            {
                                ldrs[0]["qty"] = Convert.ToSingle(ldrs[0]["qty"].ToString()) + Convert.ToSingle(ls[3].ToString());
                            }
                        }
                    }
                    #endregion

                    #region 料号数量 汇总
                    DataTable ldt_sk = LicPdaService.App.GetCreateDataTable();
                    for (int i = 0; i < ldt_sj.Rows.Count; i++)
                    {
                        string[] ls;
                        ls = ldt_sj.Rows[i]["master_label"].ToString().Split('|');
                        DataRow[] ldrs = null;
                        string lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "'";
                        ldrs = ldt_sk.Select(lswhere);
                        if (ldrs.Length == 0)
                        {
                            DataRow ldr = ldt_sk.NewRow();
                            ldr["asn"] = App.sasn;
                            ldr["sku"] = ls[0].ToString();
                            ldr["qty"] = Convert.ToSingle(ldt_sj.Rows[i]["qty"].ToString());
                            ldr["userid"] = App.suserid;
                            ldr["whseid"] = App.swhseid;
                            ldt_sk.Rows.Add(ldr);
                        }
                        else
                        {
                            ldrs[0]["qty"] = Convert.ToSingle(ldrs[0]["qty"].ToString()) + Convert.ToSingle(ldt_sj.Rows[i]["qty"].ToString());
                        }

                    }
                    #endregion

                    #region 扫描上架数量判定
                    for (int i = 0; i < ldt_sk.Rows.Count; i++)
                    {
                        asn = ldt_sk.Rows[i]["asn"].ToString();
                        sku = ldt_sk.Rows[i]["sku"].ToString();
                        qty = Int32.Parse(ldt_sk.Rows[i]["qty"].ToString()); //本次上架数量
                        qty_sm = 0;

                        strSql = "SELECT sku,SUM(pre_qty) AS qty_yb,SUM(check_qty) AS qty_ybsm   FROM [LicWMS].[dbo].[ReceiptDetail]";
                        strSql += " WHERE   receiptkey='" + asn + "' and whseid='" + App.swhseid + "' and sku='" + sku + "' and pre_qty>0 GROUP BY sku";
                        DataTable ldt_yb = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");

                        if (ldt_yb.Rows.Count > 0)
                        {
                            //预报数量
                            qty_yb = Int32.Parse(ldt_yb.Rows[0]["qty_yb"].ToString().Substring(0, ldt_yb.Rows[0]["qty_yb"].ToString().Length - 3));
                           // qty_yb = Int32.Parse(ldt_yb.Rows[0]["qty_yb"].ToString());

                            strSql = "SELECT sku,SUM(act_qty) AS qty_sm   FROM [LicWMS].[dbo].[ReceiptDetail]";
                            strSql += " WHERE   receiptkey='" + asn + "' and whseid='" + App.swhseid + "' and sku='" + sku + "' AND ( loc !='') AND pre_qty=0  GROUP BY sku";
                            DataTable ldt_sm = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");
                            if (ldt_sm.Rows.Count > 0)
                            {
                                //料号已上架数量
                                qty_sm = Int32.Parse(ldt_sm.Rows[0]["qty_sm"].ToString().Substring(0, ldt_sm.Rows[0]["qty_sm"].ToString().Length - 3));
                            }
                            if (qty + qty_sm > qty_yb)
                            {
                                for (int j = 0; j < ldt_dy.Rows.Count; j++)
                                {
                                    ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号:" + sku + "的预报数量" + qty_yb.ToString() + "小于 累计上架扫描数量总和:" + (qty + qty_sm).ToString()+ " - 提示来自 上架数量卡控"; 
                                }
                                return GetPrint_Return(ldt_dy);
                            }
                        }
                        else
                        {
                            for (int j = 0; j < ldt_dy.Rows.Count; j++)
                            {
                                ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号：" + sku + " 没有维护预报信息，无法进行PDA验收&上架 - 提示来自 料号上架卡控";
                            }
                            return GetPrint_Return(ldt_dy);
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    App.Log("Print", ex.Message.ToString());
                    throw;
                }
            }
            #endregion

            if (Check.status_check != "true")
            {
                for (int j = 0; j < ldt_dy.Rows.Count; j++)
                {
                    ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：" + Check.status_check;
                }
                return GetPrint_Return(ldt_dy);
            }

            for (int k = 0; k < ldt_dy.Rows.Count; k++)
            {
                //if (ldt_dy.Rows[k]["storage"].ToString().Trim().Length != 6)
                //{
                //    ldt_dy.Rows[k]["msg"] = "扫描的库位" + ldt_dy.Rows[k]["storage"].ToString() + "不合法，库位必须为字母开头。可能原因：PDA上存在冲突软件3505导致字符丢失！";
                //    return GetPrint_Return(ldt_dy);
                //}
                strSql = "SELECT loc FROM [dbo].[Location] where loc='" + ldt_dy.Rows[k]["storage"].ToString() + "' AND whseid='" + App.swhseid + "'";
                DataTable dtlocnew = App.GetDataTable(App.DBconWMS, strSql, "LOC");
                if (dtlocnew.Rows.Count <= 0)
                {
                    ldt_dy.Rows[k]["msg"] = "扫描的库位" + ldt_dy.Rows[k]["storage"].ToString() + "不合法。可能原因：1、库位无效 2、扫描库位不隶属登录账号所在的厂别"+App.swhseid;
                    return GetPrint_Return(ldt_dy);
                }

                strSql = "SELECT KBPO#,KBPT#,KBISTS FROM [LicWMS].[dbo].[CMS_AD_POI] WHERE KBPO#='" + ldt_dy.Rows[k]["lottable02"].ToString() + "' AND KBPT#='" + ldt_dy.Rows[k]["sku"].ToString() + "'";
                DataTable ldt_cm = App.GetDataTable(App.DBconWMS, strSql, "CMS_AD_POI");
                if (ldt_cm.Rows.Count <= 0)
                {
                    ldt_dy.Rows[k]["msg"] = "料号:" + ldt_dy.Rows[k]["sku"].ToString() + "与订单号:" + ldt_dy.Rows[k]["lottable02"].ToString() + "对应关系错误，无法验收&上架";
                    return GetPrint_Return(ldt_dy);
                }
            }


            //获取行号
            int lnline_no = ldt_asn_detail.Rows.Count; //old 保留
            strSql = "SELECT MAX(line_no) AS line_no FROM [LicWMS].[dbo].[ReceiptDetail] WHERE   receiptkey='" + App.sasn + "' and whseid='" + App.swhseid + "'";
            DataTable dtln = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");
            if (dtln.Rows.Count > 0)
            {
                lnline_no = Int32.Parse(dtln.Rows[0]["line_no"].ToString()); //new 更新
            }

            try
            {
                strSql = "DECLARE @key varchar(50) exec pro_GetLpn '" + ldt_dy.Rows[0]["whseid"].ToString() + "','PLT','托盘号',@key OUTPUT select @key as lpn";
                DataTable dtPro = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                if (dtPro.Rows.Count > 0)
                {
                    lspn = dtPro.Rows[0]["lpn"].ToString();
                }
                string lsputaway_no = "";
                strSql = "select putaway_no from recputawaydetail where whseid='" + App.swhseid + "' and  receiptkey='" + App.sasn + "'";
                DataTable ldtputaway_no = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                if (ldtputaway_no.Rows.Count == 0)
                {
                    strSql = "DECLARE @key varchar(50) exec pro_GetLot '" + ldt_dy.Rows[0]["whseid"].ToString() + "','PUTAWAY','上架任务编号',@key OUTPUT select @key as putaway ";
                    DataTable dtLot = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                    if (dtLot.Rows.Count > 0)
                    {
                        lsputaway_no = dtLot.Rows[0]["putaway"].ToString();
                    }
                    //调用存储过程pro_GetLpn生成托盘号;
                }
                else
                {
                    lsputaway_no = ldtputaway_no.Rows[0]["putaway_no"].ToString();
                }

                for (int i = 0; i < ldt_dy.Rows.Count; i++)
                {
                    string lsdesc = "";
                    string lsunit = "";
                    string qc_man = "";
                    string qc_status = "";
                    string lslottable01 = "";
                    string lslottable02 = "";
                    string lslottable03 = "";
                    string lslottable04 = "";
                    string lslottable05 = "";
                    string lslottable06 = "";
                    string lslottable07 = "";
                    string lslottable08 = "";
                    string lslottable09 = "";
                    string lslottable10 = "";
                    string lsinvoice_no = "";
                    string lsline_no = "";
                    DataRow[] ldrs = ldt_asn_detail.Select("sku='" + ldt_dy.Rows[i]["sku"].ToString() + "' and lottable01='" + ldt_dy.Rows[i]["lottable01"].ToString() + "' and lottable02='" + ldt_dy.Rows[i]["lottable02"].ToString() + "'");
                    ldt_dy.Rows[i]["lpn"] = lspn;
                    if (ldrs.Length == 0)
                    {
                        for (int j = 0; j < ldt_dy.Rows.Count; j++)
                        {
                            ldt_dy.Rows[j]["msg"] = "ASN预报中未发现符合条件的 ASN+料号+订单号 预报组合信息。可能原因：1、货物未在PDA上执行“到货验收” 2、系统维护的预报 ASN+料号+订单号 与 扫描的实物不匹配！";
                        }
                        return GetPrint_Return(ldt_dy);
                    }
                    else
                    {
                        try
                        {
                            lsline_no = ldrs[0]["line_no"].ToString();
                            lsdesc = ldrs[0]["skudescr"].ToString();
                            lsunit = ldrs[0]["uom"].ToString();
                            lsinvoice_no = ldrs[0]["invoice_no"].ToString();
                            qc_man = ldrs[0]["qc_man"].ToString();
                            qc_status = ldrs[0]["qc_status"].ToString();
                            lslottable01 = ldrs[0]["lottable01"].ToString();
                            lslottable02 = ldrs[0]["lottable02"].ToString();
                            lslottable03 = ldrs[0]["lottable03"].ToString();
                            lslottable04 = ldrs[0]["lottable04"].ToString();
                            lslottable05 = ldrs[0]["lottable05"].ToString();
                            lslottable06 = ldrs[0]["lottable06"].ToString();
                            lslottable07 = ldrs[0]["lottable07"].ToString();
                            lslottable08 = ldrs[0]["lottable08"].ToString();
                            lslottable09 = ldrs[0]["lottable09"].ToString();   //包装键
                            lslottable10 = ldrs[0]["lottable10"].ToString();   //供应商代码
                        }
                        catch { }

                    }
                    lnline_no += 1;
                    if (lslottable03.Trim().Length < 18)
                    {
                        lslottable03 = App.sasn.ToString() + lnline_no.ToString().PadLeft(5, '0').ToString();
                    }
                    string lscustomer_code = ldt_asn_detail.Rows[0]["customer_code"].ToString();

                    string lssql = "insert into receiptdetail(whseid,receiptkey,line_no,customer_code,sku,skudescr,loc,lpn,pre_qty,act_qty,check_qty,goodqty,ngqty,waitqty,unrec_qty,uom,qc_man,qc_status,lottable01,lottable02,lottable03,lottable04,lottable05";
                    lssql += ",lottable06,lottable07,lottable08,lottable09,lottable10,invoice_no,addwho,adddate) values( ";
                    lssql += "'" + App.swhseid + "','" + App.sasn + "','" + lnline_no.ToString().PadLeft(5, '0') + "','" + lscustomer_code + "','" + ldt_dy.Rows[i]["sku"].ToString() + "','" + lsdesc + "','" + ldt_dy.Rows[i]["storage"].ToString() + "'";
                    lssql += ",'" + lspn + "',0," + ldt_dy.Rows[i]["qty"].ToString() + "," + ldt_dy.Rows[i]["qty"].ToString() + "," + ldt_dy.Rows[i]["qty"].ToString() + ",0,0," + ldt_dy.Rows[i]["qty"].ToString() + ",'" + lsunit + "','"+qc_man+"','"+qc_status+"','" + lslottable01 + "','" + lslottable02 + "','" + lslottable03 + "'";
                    lssql += ",'" + lslottable04 + "','" + lslottable05 + "','" + lslottable06 + "','" + lslottable07 + "','" + lslottable08 + "','" + lslottable09 + "','" + lslottable10 + "','" + lsinvoice_no + "','" + App.suserid + "-PDA','" + System.DateTime.Now.ToString() + "')";
                    sqllists.Add(lssql);

                    string lsSql1 = "insert into recputawaydetail(whseid,putaway_no,receiptkey,customer_code,line_no,sku,lottable01,loc,lpn,qty,addwho,adddate,product,status) values ";
                    lsSql1 += "('" + App.swhseid + "','" + lsputaway_no + "','" + App.sasn + "','" + lscustomer_code + "','" + lnline_no.ToString().PadLeft(5, '0') + "','" + ldt_dy.Rows[i]["sku"].ToString() + "','" + lslottable01 + "','" + ldt_dy.Rows[i]["storage"].ToString() + "','" + lspn + "'," + ldt_dy.Rows[i]["qty"].ToString() + ",'" + App.suserid + "','" + DateTime.Now.ToString() + "',0,0)";
                    sqllists.Add(lsSql1);

                    if(qty + qty_sm > 0) //以实际扫描量写入
                    {
                        string lsSql2 = "update receiptdetail set status='04',act_qty=" + (qty + qty_sm).ToString() + " where whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "' and line_no='" + lsline_no + "' ";
                        sqllists.Add(lsSql2);
                    }
                    else
                    {
                        string lsSql2 = "update receiptdetail set status='04',act_qty=act_qty+" + ldt_dy.Rows[i]["qty"].ToString() + " where whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "' and line_no='" + lsline_no + "' ";
                        sqllists.Add(lsSql2);
                    }

                    string lsSql4 = "update receipt set status='04'" + " where whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "'  ";
                    sqllists.Add(lsSql4);

                }
                if (ldtputaway_no.Rows.Count == 0)
                {
                    string lsSql3 = "insert into RecPutaway(whseid,putaway_no, product,status,addwho,adddate) values ";
                    lsSql3 += "('" + App.swhseid + "','" + lsputaway_no + "',0,0,'" + App.suserid + "','" + DateTime.Now.ToString() + "')";
                    sqllists.Add(lsSql3);
                }

                App.ExecuteSQLScriptLists(sqllists, App.DBconWMS);
            }
            catch (Exception ex)
            {
                App.Log("Print1", ex.Message.ToString());
                for (int j = 0; j < ldt_dy.Rows.Count; j++)
                {
                    ldt_dy.Rows[j]["msg"] = ex.ToString();
                }
                return GetPrint_Return(ldt_dy);
                throw;
            }

            return GetPrint_Return(ldt_dy);
        }



        //条印--返回值
        private static string GetPrint_Return(DataTable ldt)
        {
            //排除重复项
            for (int i = ldt.Rows.Count - 2; i > 0; i--)
            {
                DataRow[] ldrs = ldt.Select("sku='" + ldt.Rows[i]["sku"].ToString() + "'");
                if (ldrs.Length > 1)
                {
                    ldt.Rows.RemoveAt(i);
                }
            }
            ldt.AcceptChanges();
            XmlDocument doc = new XmlDocument();//返回报文
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement rootRet = doc.CreateElement("NewDataSet");

            for (int i = 0; i < ldt.Rows.Count; i++)
            {
                XmlNode printNodes = doc.CreateElement("print_return");//返回用户节点
                XmlElement lxeuserid = doc.CreateElement("userid");
                XmlElement lxeasn = doc.CreateElement("asn");
                XmlElement lxewhseid = doc.CreateElement("whseid");
                XmlElement lxemaster_label = doc.CreateElement("master_label");
                XmlElement lxestorage = doc.CreateElement("storage");
                XmlElement lxereturn_status = doc.CreateElement("return_status");
                XmlElement lxesku = doc.CreateElement("sku");
                XmlElement lxebatch = doc.CreateElement("batch");
                XmlElement lxelpn = doc.CreateElement("lpn");
                XmlElement lxeqty = doc.CreateElement("qty");
                XmlElement lxepage = doc.CreateElement("page");

                lxeuserid.InnerText = ldt.Rows[i]["userid"].ToString();
                lxeasn.InnerText = ldt.Rows[i]["asn"].ToString();
                lxewhseid.InnerText = ldt.Rows[i]["whseid"].ToString();
                lxemaster_label.InnerText = ldt.Rows[i]["master_label"].ToString();
                lxestorage.InnerText = ldt.Rows[i]["storage"].ToString();
                lxereturn_status.InnerText = ldt.Rows[i]["msg"].ToString();
                lxesku.InnerText = ldt.Rows[i]["sku"].ToString();
                lxeqty.InnerText = ldt.Rows[i]["qty"].ToString();
                lxebatch.InnerText = ldt.Rows[i]["lottable01"].ToString();
                lxelpn.InnerText = ldt.Rows[i]["lpn"].ToString();
                lxepage.InnerText = Math.Ceiling(Convert.ToDecimal(i + 1) / 1).ToString();

                printNodes.AppendChild(lxeuserid);
                printNodes.AppendChild(lxeasn);
                printNodes.AppendChild(lxewhseid);
                printNodes.AppendChild(lxemaster_label);
                printNodes.AppendChild(lxereturn_status);
                printNodes.AppendChild(lxestorage);
                printNodes.AppendChild(lxesku);
                printNodes.AppendChild(lxeqty);
                printNodes.AppendChild(lxebatch);
                printNodes.AppendChild(lxelpn);
                printNodes.AppendChild(lxepage);
                rootRet.AppendChild(printNodes);


            }

            doc.AppendChild(rootRet);
            return doc.InnerXml;
        }

    }


}
